#ifndef VIDEODECODETHREAD_H
#define VIDEODECODETHREAD_H

#include <QObject>
#include <QThread>
#include <QWaitCondition>
#include <QMutex>
#include "ffinclude.h"

class VideoPlayer;

/*
    解码线程
*/
class DecodeThread : public QThread
{
    Q_OBJECT
public:
    enum DecodeType{
        VideoDecode,
        AudioDecode,
        TextDecode
    };

    explicit DecodeThread(DecodeType type,VideoPlayer *player);

    void play();
    void pause();
    void stop();

signals:
    void inited();
    void error(const QString& errMsg);

protected:
    void run();

private:
    bool openDecoder();
    void closeDecoder();
    void decodeNextAVFrame(AVFrame *frame, bool* valid);
    void decodeNextAVText(AVSubtitle *text, bool* valid);

private:
    DecodeType m_decodeType;
    VideoPlayer* m_player;

    bool m_reqStop;
    bool m_reqPause;
    QString m_lastErrorMsg;

    AVPacket m_pkt;
    int m_pktSerial;
    bool m_hasPendingPacket;
    int64_t m_audioStartPts;
    AVRational m_audioStartPtsTimebase;
    int64_t m_audioNextPts;
    AVRational m_audioNextPtsTimebase;
};

#endif // VIDEODECODETHREAD_H
